package com.label.Web;

import com.label.Config.MyBatis.IdGenerator;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Query.DataImportQuery;
import com.label.Service.DataImportService;
import lombok.extern.slf4j.Slf4j;
import oracle.net.ano.DataIntegrityService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Author 75046
 * @Date 2022/5/17 15:17
 */

@RestController
@Slf4j
public class DataImportController {

    @Resource
    private DataImportService dataImportService;

    @Value("${file.path}")
    public String path;

    @RequestMapping("/dataImport")
    public ResponseBean dataImport(@RequestBody DataImportQuery dataImportQuery){
        try{
            return new ResponseBean(200,"成功",dataImportService.dataImport(dataImportQuery.getTableName(),dataImportQuery.getFileName()));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(400,"请检查数据格式",null);
        }
    }

    @RequestMapping("/uploadData")
    public ResponseBean uploadData(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseBean(500,"上传失败，请选择文件",null);
        }

        File dest = new File(path + IdGenerator.uuid()+file.getOriginalFilename());
        try {
            file.transferTo(dest);
            return new ResponseBean(200,"上传成功",dest.getName());
        } catch (IOException e) {
            return new ResponseBean(400,"上传失败",null);
        }
    }
}
